package com.xiaoming.backend.service.impl;

import com.xiaoming.backend.dao.mapper.UserPOMapper;
import com.xiaoming.backend.entity.db.example.UserPOExample;
import com.xiaoming.backend.entity.db.po.UserPO;
import com.xiaoming.backend.entity.orm.User;
import com.xiaoming.backend.util.EntityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {
    private final UserPOMapper userMapper; // 替换 UserRepository

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        user.initRole();
        return org.springframework.security.core.userdetails.User.builder()
                .username(user.getUserName())
                .password(user.getPassWard())
                .roles(user.getRole())
                .build();
    }
}
